﻿using Microsoft.EntityFrameworkCore.Migrations;

namespace QianJiaInfo.Migrations
{
    public partial class InitialCreate : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "Addresses",
                columns: table => new
                {
                    AddressId = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Context = table.Column<string>(maxLength: 10, nullable: false),
                    Effective = table.Column<bool>(nullable: false, defaultValue: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Addresses", x => x.AddressId);
                });

            migrationBuilder.CreateTable(
                name: "PersonRelationships",
                columns: table => new
                {
                    PersonRelationshipId = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Relationship = table.Column<string>(nullable: false),
                    Effective = table.Column<bool>(nullable: false, defaultValue: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PersonRelationships", x => x.PersonRelationshipId);
                });

            migrationBuilder.CreateTable(
                name: "Residences",
                columns: table => new
                {
                    ResidenceId = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    ResidenceAddressAddressId = table.Column<int>(nullable: true),
                    Effective = table.Column<bool>(nullable: false, defaultValue: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Residences", x => x.ResidenceId);
                    table.ForeignKey(
                        name: "FK_Residences_Addresses_ResidenceAddressAddressId",
                        column: x => x.ResidenceAddressAddressId,
                        principalTable: "Addresses",
                        principalColumn: "AddressId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "Residents",
                columns: table => new
                {
                    ResidentId = table.Column<int>(nullable: false)
                        .Annotation("Sqlite:Autoincrement", true),
                    Name = table.Column<string>(maxLength: 10, nullable: false),
                    CardId = table.Column<string>(maxLength: 18, nullable: true),
                    ResidenceBlongResidenceId = table.Column<int>(nullable: true),
                    State = table.Column<int>(nullable: false),
                    RelationshipPersonRelationshipId = table.Column<int>(nullable: true),
                    PhoneNumber = table.Column<string>(maxLength: 11, nullable: true),
                    WorkAddress = table.Column<string>(nullable: true),
                    Remark = table.Column<string>(nullable: true),
                    HoseHold = table.Column<bool>(nullable: false, defaultValue: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_Residents", x => x.ResidentId);
                    table.ForeignKey(
                        name: "FK_Residents_PersonRelationships_RelationshipPersonRelationshipId",
                        column: x => x.RelationshipPersonRelationshipId,
                        principalTable: "PersonRelationships",
                        principalColumn: "PersonRelationshipId",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_Residents_Residences_ResidenceBlongResidenceId",
                        column: x => x.ResidenceBlongResidenceId,
                        principalTable: "Residences",
                        principalColumn: "ResidenceId",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateIndex(
                name: "IX_Residences_ResidenceAddressAddressId",
                table: "Residences",
                column: "ResidenceAddressAddressId");

            migrationBuilder.CreateIndex(
                name: "IX_Residents_RelationshipPersonRelationshipId",
                table: "Residents",
                column: "RelationshipPersonRelationshipId");

            migrationBuilder.CreateIndex(
                name: "IX_Residents_ResidenceBlongResidenceId",
                table: "Residents",
                column: "ResidenceBlongResidenceId");
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "Residents");

            migrationBuilder.DropTable(
                name: "PersonRelationships");

            migrationBuilder.DropTable(
                name: "Residences");

            migrationBuilder.DropTable(
                name: "Addresses");
        }
    }
}
